package com.xj.edu.hotel.controller;
import com.xj.edu.hotel.domain.Room;
import com.xj.edu.hotel.domain.Hotel;
import com.xj.edu.hotel.service.HotelService;
import com.xj.edu.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/hotelSearch")
public class HotelSearchController {
    @Autowired
    private HotelService hotelService;
    @Autowired
    private RoomService roomService;
    /**
     * 酒店搜索
     */
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public  String search(String searchhotel, Model model){
        List<Hotel> hotel=hotelService.searchHotelName(searchhotel);
        model.addAttribute("hotel",hotel);
        return  "gallery";
    }

    /**
     *房间信息展示
     */
    @RequestMapping(value = "/searchRoom",method = RequestMethod.GET)
    public  String searchRoom(String id ,Model model){
       List<Room> room=roomService.searchRoom(id);
       List<Room> room1 = roomService.hotelComment(id);
       model.addAttribute("room",room);
       model.addAttribute("room1",room1);
        return "rooms";
    }
    /**
     * 房间详情
     */
    @RequestMapping("/detail")
    public String Detail(String id,Model model){
        Room room = roomService.findRoomByid(id);
       model.addAttribute("room",room);
            return "detail";
    }
    /**
     *  按预定时间最新查找酒店
     */
    @RequestMapping("searchHotelByOrderTime")
    public String searchHotelByOrderTime(Model model)throws ParseException {
        SimpleDateFormat formatDate = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss");
        Date currentTime =  new Date();
        String time = formatDate.format(currentTime);
        Date date = formatDate.parse(time);
        List<Hotel> hotels = hotelService.searchHotelByOrderTime(date);
        model.addAttribute("hotels",hotels);
        return "index";
    }
    /**
     * 按预定数量最多查找酒店
     */
    @RequestMapping("searchHotelByOrderCount")
    public String searchHotelByOrderCount(Model model){
      List<Hotel> hotels = hotelService.searchHotelByOrderCount();
      model.addAttribute("hotels",hotels);
      if(hotels.size() != 0){
          return "index";
      }else {
        return "redirect:/toUserLogin";
      }
    }

    /**
     * 特色酒店
     */
    @RequestMapping("/hotelType")
    @ResponseBody
    public List<Hotel> selectHotel() {
        List<Hotel> hotelList= hotelService.findhotelType();
        return hotelList;
    }
    /**
    特色酒店展示
     */
    @RequestMapping(value = "/hotelShow",method = RequestMethod.GET)
    public  String hotelShow(String hotelType ,Model model){
        List<Hotel> hotel=hotelService.hotelShow(hotelType);
        model.addAttribute("hotel",hotel);
        return "specialHotel";
    }
    /**
     *  按房间价格区间搜索酒店
     */
    @RequestMapping(value = "/Pricesearch",method = RequestMethod.POST)
    public  String Pricesearch(BigDecimal minPrice,BigDecimal maxPrice, Model model){
        List<Hotel> hotel=hotelService.priceSearch(minPrice,maxPrice);
        model.addAttribute("hotel",hotel);
        return  "gallery";
    }

    /**
     *  地图
     */
    @RequestMapping("mapsearch")
    public String mapsearch(){
        return "map";
    }

}
